package com.yzdmw.toutiao.wheeldisk.ui;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.yzdmw.toutiao.login.data.LoginRepository;
import com.yzdmw.toutiao.model.News;
import com.yzdmw.toutiao.wheeldisk.data.WheeldiskRepository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class WheeldiskViewModel extends ViewModel {
    public static final String TAG = "WheeldiskViewModel";
    private static final boolean DEBUG=false;
    // 状态存储
    private final WheeldiskRepository wheeldiskRepository;

    public WheeldiskViewModel() {
        this.wheeldiskRepository = WheeldiskRepository.getInstance();
//        for(int i=0;i<50;i++){
//            news.add(new News("test"+i+"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz","contexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjcontexdjsflkajdskfjlksdjlgkjdskjj,contexdjsflkajdskfjlksdjlgkjdskjj,,,contexdjsflkajdskfjlksdjlgkjdskjjcontexdjsflkajdskfjlksdjlgkjdskjjcontexdjsflkajdskfjlksdjlgkjdskjj.."));
//        }
    }

    List<News> news = new ArrayList<>();
    private MutableLiveData<Integer> _refreshError = new MutableLiveData<>(null);
    public LiveData<Integer> refreshError() { return _refreshError; }

    private int page = 1;
    public void refreshNews(String type, boolean clear) {
        wheeldiskRepository.publicTypeNews(type,page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()) //LiveData的更新需要在ui线程
                .subscribe(response -> {
                    if (DEBUG)
                        Log.d(TAG,""+response.success+response.code+response.data);
                    if (response.success && response.code == 200) {
                        page++;
                        if (clear){
                            news.clear();
                            page = 1;
                        }
                        news.addAll(response.data);
                        _refreshError.setValue(null);
                    } else {
                        _refreshError.setValue(response.code);
                    }
        });
    }

    public void itemMove(int fromPosition, int toPosition) {
        for(int i = fromPosition ; i > toPosition ; i--)
            Collections.swap(news,i,i-1);
    }
}